package com.ruoyi.peoplegroup.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.peoplegroup.domain.Partnerinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.peoplegroup.mapper.PartnercategoryMapper;
import com.ruoyi.peoplegroup.domain.Partnercategory;
import com.ruoyi.peoplegroup.service.IPartnercategoryService;

/**
 * 合作单位类别Service业务层处理
 * 
 * @author gw
 * @date 2025-01-08
 */
@Service
public class PartnercategoryServiceImpl implements IPartnercategoryService 
{
    @Autowired
    private PartnercategoryMapper partnercategoryMapper;

    /**
     * 查询合作单位类别
     * 
     * @param categoryId 合作单位类别主键
     * @return 合作单位类别
     */
    @Override
    public Partnercategory selectPartnercategoryByCategoryId(Long categoryId)
    {
        return partnercategoryMapper.selectPartnercategoryByCategoryId(categoryId);
    }

    /**
     * 查询合作单位类别列表
     * 
     * @param partnercategory 合作单位类别
     * @return 合作单位类别
     */
    @Override
    public List<Partnercategory> selectPartnercategoryList(Partnercategory partnercategory)
    {
        return partnercategoryMapper.selectPartnercategoryList(partnercategory);
    }

    /**
     * 新增合作单位类别
     * 
     * @param partnercategory 合作单位类别
     * @return 结果
     */
    @Override
    public int insertPartnercategory(Partnercategory partnercategory)
    {
        partnercategory.setCreatedBy(SecurityUtils.getUsername());
        return partnercategoryMapper.insertPartnercategory(partnercategory);
    }

    /**
     * 修改合作单位类别
     * 
     * @param partnercategory 合作单位类别
     * @return 结果
     */
    @Override
    public int updatePartnercategory(Partnercategory partnercategory)
    {
        partnercategory.setUpdatedBy(SecurityUtils.getUsername());
        return partnercategoryMapper.updatePartnercategory(partnercategory);
    }

    /**
     * 批量删除合作单位类别
     * 
     * @param categoryIds 需要删除的合作单位类别主键
     * @return 结果
     */
    @Override
    public int deletePartnercategoryByCategoryIds(Long[] categoryIds)
    {
        int updatedCount = 0;  // 计数器，记录成功更新的记录数

        for (Long id : categoryIds){
            // 获取合作单位信息
            Partnercategory partnercategory = selectPartnercategoryByCategoryId(id);

            if (partnercategory != null) {
                // 设置删除相关信息
                partnercategory.setDeletedBy(SecurityUtils.getUsername());
                partnercategory.setDeletedAt(new Date());
                partnercategory.setIsDeleted(1);  // 标记为已删除

                // 更新合作单位信息
                int result =partnercategoryMapper.updatePartnercategory(partnercategory);
                if (result > 0) {
                    updatedCount++;  // 如果更新成功，计数器加1
                }
            }
        }

        return updatedCount;
    }

    /**
     * 删除合作单位类别信息
     * 
     * @param categoryId 合作单位类别主键
     * @return 结果
     */
    @Override
    public int deletePartnercategoryByCategoryId(Long categoryId)
    {
        return partnercategoryMapper.deletePartnercategoryByCategoryId(categoryId);
    }
}
